<template>
  <div class="RulelibraryListCom">
    <jxtech-page-table ref="RulelibraryListTableRef" row-key="ruleCode" fit="full" :table-column="RulelibraryListState.tableColumn"
                       :load-data="RulelibraryListState.loadData" :exp-data="RulelibraryListState.expData"
                       @selection-change="RulelibraryListState.tableSelectionChange">
      <template #header>
        <div class="jxtech-page-table-header-flex">
          <jxtech-search-form :query-column="RulelibraryListState.queryColumn" :query-form="RulelibraryListState.queryParam" :col-span="6"
                              @handle-search="RulelibraryListState.handleSearch">

          </jxtech-search-form>
        </div>
      </template>

      <template #actionSlot="{ row }">
        <el-button type="" @click="RulelibraryListState.edithandler(row)">
          <jxtech-svg-icon icon="#icon-edit" class="pointer"></jxtech-svg-icon>&nbsp;编 辑
        </el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button type="danger" @click="RulelibraryListState.deletehandler(row)">
          <jxtech-svg-icon icon="#icon-delete" class="pointer"></jxtech-svg-icon>&nbsp;删 除
        </el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button type="success" @click="RulelibraryListState.playhandler(row)">
          <jxtech-svg-icon icon="#icon-play1" class="pointer"></jxtech-svg-icon>&nbsp;执 行
        </el-button>
      </template>

    </jxtech-page-table>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue"
import rulelibraryServiceApi from "/@/api/personnel/bean/rulelibrary"
import { ElNotification, ElMessageBox, ElMessage } from "element-plus"
import { useRouter } from "vue-router"
import { RulelibraryEditUrl } from "/@/views/personnel/mapping.json"
import { log } from "console"
const router = useRouter()
const RulelibraryListTableRef = ref()
const RulelibraryListState = reactive({
  handleSearch: () => RulelibraryListTableRef.value.loadTable(),
  queryColumn: [
    {
      name: "ruleCode",
      type: "input",
      label: "规则编码",
      placeholder: "规则编码"
    }
  ],
  queryParam: { likeAll: null },
  selectionData: [] as any[],
  tableColumn: [
    { label: "序号", width: "50", type: "selection" },
    { label: "序号", width: "50", type: "index" },
    { prop: "ruleCode", label: "规则编码" },
    { prop: "businessDomain", label: "业务域" },
    { prop: "systemName", label: "源端库名" },
    { prop: "ruleTableCh", label: "表中文名" },
    { prop: "ruleTableEn", label: "表英文名" },
    { prop: "ruleColCh", label: "字段中文名" },
    { prop: "ruleColEn", label: "字段英文名" },
    { prop: "ruleType", label: "评价指标" },
    { prop: "ruleName", label: "校验规则" },
    { label: "操作", slot: "actionSlot", width: 300 }
  ],
  //表格数据加载的方法
  loadData: (params: any) => {
    return rulelibraryServiceApi.queryPage(
      { refCols: "default" },
      {
        ...params,
        ...RulelibraryListState.queryParam
      }
    )
  },
  /**
   * 表格导出excel的方法
   */
  expData: (params: any) => {
    return rulelibraryServiceApi.exportData(null, {
      ...params,
      ...RulelibraryListState.queryParam
    })
  },
  edithandler: (row: any) => {
    router.push({ path: RulelibraryEditUrl, query: { ruleCode: row.ruleCode } })
  },
  deletehandler: (row: any) => {
    ElMessageBox.confirm("您确定要删除选择的数据么?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(() => {
        rulelibraryServiceApi.delete(row.ruleCode, null).then(response => {
          ElNotification({
            title: "删除成功",
            message: "规则编码-" + row.ruleCode,
            type: "success"
          })
          RulelibraryListTableRef.value.loadTable()
        })
      })
      .catch(() => {
        // ElMessage({
        //   type: 'info',
        //   message: 'Delete canceled',
        // })
      })
  },
  playhandler: (row: any) => {
    ElMessageBox.confirm("是否执行该规则信息？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "info"
    })
      .then(() => {
        rulelibraryServiceApi
          .executeSQL({ ruleCode: row.ruleCode }, null)
          .then((response: any) => {
            // console.log(response)
            ElNotification({
              title: response.data.msg,
              message: "ruleCode-" + row.ruleCode,
              type: "success"
            })
          })
      })
      .catch(() => {
        // ElMessage({
        //   type: 'info',
        //   message: 'Delete canceled',
        // })
      })
  },
  tableSelectionChange: (dataList: any[]) => {
    RulelibraryListState.selectionData = dataList
  },
  multideletehandler: () => {
    if (!RulelibraryListState.selectionData.length) {
      return ElNotification({
        title: "错误",
        message: "没有选择任何数据",
        type: "error"
      })
    }
    ElMessageBox.confirm("您确定要删除选择的数据么?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(() => {
        const idsarray: string[] = []
        RulelibraryListState.selectionData.forEach(item => {
          idsarray.push(item.ruleCode)
        })
        rulelibraryServiceApi.saveMultiDelete(idsarray, null).then(response => {
          ElNotification({
            title: "删除成功",
            message: "ruleCode-" + idsarray,
            type: "success"
          })
          RulelibraryListTableRef.value.loadTable()
        })
      })
      .catch(() => {
        // ElMessage({
        //   type: 'info',
        //   message: 'Delete canceled',
        // })
      })
  }
})
onMounted(() => {
  RulelibraryListTableRef.value.loadTable()
})
defineExpose({
  exposemultideletehandler: () => RulelibraryListState.multideletehandler()
})
</script>

<style lang="scss" scoped>
</style>
